package com.unittec.drainage.provider.auth.excel.export;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import com.unittec.drainage.provider.common.Md5;
import com.unittec.drainage.provider.common.ServiceIdConstants;
import com.unittec.drainage.provider.entity.ExportTaskEntity;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.scope.ThreadLocalScope;
import com.unittec.drainage.provider.service.HystrixRestTemplateService;
import com.unittec.drainage.provider.utils.CopyUtils;
import com.unittec.drainage.sdk.auth.domain.ExportTaskConfig;
import com.unittec.drainage.sdk.auth.request.Req101901;
import com.unittec.drainage.sdk.auth.request.Req101902;
import com.unittec.drainage.sdk.auth.request.Req101903;
import com.unittec.drainage.sdk.auth.response.Res101901;
import com.unittec.drainage.sdk.auth.response.Res101902;
import com.unittec.drainage.sdk.auth.response.Res101903;
import com.unittec.drainage.sdk.root.common.FieldConstants;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;

/**
 * @Description:
 * @Author: wuxinwei
 * @CreateDate: 2019/11/15 16:17
 */
@Service
public class ExportTaskService {

    @Autowired
    private HystrixRestTemplateService hystrixRestTemplateService;

    @Autowired
    private Log log;

    public void save(ExportTaskEntity entity) {
        log.sysInfoLog("save ", "begin");
        try {
            GatewayRequest<Req101901> getWay = new GatewayRequest<>();
            Req101901 req = new Req101901();
            ExportTaskConfig exportTaskConfig = new ExportTaskConfig();
            CopyUtils.copyProperties(entity, exportTaskConfig);
            req.setExportTaskConfig(exportTaskConfig);
            getWay.setContent(req);
            getWay.setMethod("101901");
            Result<Res101901> result = hystrixRestTemplateService.exchange(
                Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID), ServiceIdConstants.AUTH_101901, getWay,
                ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101901>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "新增任务失败");
                throw new BusinessException("新增任务失败");
            }
        } catch (BeansException e) {
            log.sysErrLog("save", "新增任务失败", e);
            throw new BusinessException("新增任务失败");
        }

        log.sysInfoLog("save", "end");
    }

    public void updateById(ExportTaskEntity entity) {
        log.sysInfoLog("updateById ", "begin");
        try {
            GatewayRequest<Req101902> getWay = new GatewayRequest<>();
            Req101902 req = new Req101902();
            ExportTaskConfig exportTaskConfig = new ExportTaskConfig();
            CopyUtils.copyProperties(entity, exportTaskConfig);
            req.setExportTaskConfig(exportTaskConfig);
            getWay.setContent(req);
            getWay.setMethod("101902");
            Result<Res101902> result = hystrixRestTemplateService.exchange(
                Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID), ServiceIdConstants.AUTH_101902, getWay,
                ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101902>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "修改任务失败");
                throw new BusinessException("修改任务失败");
            }

        } catch (BeansException e) {
            log.sysErrLog("updateById", "修改任务失败", e);
            throw new BusinessException("修改任务失败");
        }
        log.sysInfoLog("save", "end");
    }

    public ExportTaskEntity getById(String id) {
        log.sysInfoLog("getById ", "begin");
        ExportTaskEntity exportTaskEntity = new ExportTaskEntity();
        try {
            GatewayRequest<Req101903> getWay = new GatewayRequest<>();
            Req101903 req = new Req101903();
            req.setId(id);
            getWay.setContent(req);
            getWay.setMethod("101903");
            Result<Res101903> result = hystrixRestTemplateService.exchange(
                Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID), ServiceIdConstants.AUTH_101903, getWay,
                ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101903>>() {});

            if (result.isSuccess()) {
                if (result.getData() != null && result.getData().getExportTaskConfig() != null) {
                    ExportTaskConfig entity = result.getData().getExportTaskConfig();
                    CopyUtils.copyProperties(entity, exportTaskEntity);
                } else {
                    log.error(getWay, BizCode.QUERY_ERROR.getCode(), "获取任务详情失败");
                    throw new BusinessException("获取任务详情失败");
                }
            } else {
                log.error(getWay, BizCode.QUERY_ERROR.getCode(), "获取任务详情失败");
                throw new BusinessException("获取任务详情失败");
            }

        } catch (BeansException e) {
            log.sysErrLog("getById", "修改任务失败", e);
            throw new BusinessException("修改任务失败");
        }
        log.sysInfoLog("getById", "end");
        return exportTaskEntity;
    }
}
